package com.ayoka.adapter;

import com.ayoka.R;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.QuestionOption;
import com.ayoka.mvp.CourseEstimateQuestionMVP.CourseEstimateQuestionModel.CourseEstimateQuestionMultiEntity;
import com.jcp.baselibrary.brvah.BaseMultiItemQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;

/**
 * Created by jinchunping on 2018/4/11.
 */

public class CourseEstimateQuestionMultiAdapter extends BaseMultiItemQuickAdapter<CourseEstimateQuestionMultiEntity, BaseViewHolder> {

    public CourseEstimateQuestionMultiAdapter() {
        super(null);
        addItemType(CourseEstimateQuestionMultiEntity.QUESTION, R.layout.multi_item_course_estimate_question_question);
        addItemType(CourseEstimateQuestionMultiEntity.OPTION, R.layout.multi_item_course_estimate_question_option);
        addItemType(CourseEstimateQuestionMultiEntity.BUTTON, R.layout.multi_item_course_estimate_question_button);
    }

    @Override
    protected void convert(BaseViewHolder holder, CourseEstimateQuestionMultiEntity item) {
        if (item.getItemType() == CourseEstimateQuestionMultiEntity.QUESTION) {
            holder.setText(R.id.tv_question, (String) item.getData());
        } else if (item.getItemType() == CourseEstimateQuestionMultiEntity.OPTION) {
            QuestionOption questionOption = (QuestionOption) item.getData();
            holder.setText(R.id.tv_option, questionOption.Option)
                    .setSelected(R.id.tv_option, questionOption.isSelected)
                    .addOnClickListener(R.id.tv_option);
        } else if (item.getItemType() == CourseEstimateQuestionMultiEntity.BUTTON) {
            holder.setText(R.id.btn_operate, R.string.last_question);
            holder.addOnClickListener(R.id.btn_operate);
        }
    }

    public void setSingleChoice(int position) {
        for (int i = 0; i < getData().size(); i++) {
            CourseEstimateQuestionMultiEntity multiEntity = getItem(i);
            if (multiEntity == null) continue;
            if (multiEntity.getItemType() == CourseEstimateQuestionMultiEntity.OPTION){
                QuestionOption questionOption = (QuestionOption) multiEntity.getData();
                questionOption.isSelected = i == position;
            }
        }
        notifyDataSetChanged();
    }
}
